#include "widget.h"
#include "ui_widget.h"

#include <QCursor>
#include <QMouseEvent>
#include <QPixmap>
#include <QDebug>
#include <QEvent>
#include <QWheelEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建光标对象
    QCursor cursor;
    // 修改鼠标形状
    cursor.setShape(Qt::OpenHandCursor);
    // 设置鼠标
    setCursor(cursor);
}

Widget::~Widget()
{
    delete ui;
}

// 鼠标的按下事件
void Widget::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton){ // 如果是鼠标左键按下
        QCursor cursor;
        cursor.setShape(Qt::ClosedHandCursor);
        QApplication::setOverrideCursor(cursor);
        offset = event->globalPos() - pos();
    }else if(event->buttons) == Qt::RightButton){ // 如果是鼠标右键按下
        QCursor cursor(QPixmap(":/res/mouse.png"));
        QApplication::setOverrideCursor(cursor);
    }
}

// 鼠标的释放事件
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    QApplication::restoreOverrideCursor();
}

// 鼠标的双击事件
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton){
        if(windowState() != Qt::WindowFullScreen){
            setWindowState(Qt::WindowFullScreen);
        }else{
            setWindowState(Qt::WindowNoState);
        }
    }
}

// 鼠标拖动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    // 移动过程中判断鼠标是左键点击并且移动，那么要用buttons，返回的是鼠标状态的集合
    if(event->buttons() & Qt::LeftButton){
        // 获取窗口应当移动到的位置
        QPoint windowPos = event->globalPos() - offset;
        this->move(windowPos);
    }
}

// 滚轮事件
void Widget::wheelEvent(QWheelEvent *event)
{
    // 鼠标滚动远离使用者放大 textEdit
    if(event->delta() > 0){
        qDebug() << "catch wheel event delta > 0" << Qt::endl;
        ui->textEdit->zoomIn();
    }else{
        qDebug() << "catch wheel event delta < 0" << Qt::endl;
        ui->textEdit->zoomOut();
    }
}
